<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <script>
            function removeZero(str) {
                var idx = 0;
                var prefix = '';
                if(str.charAt(0) == '-' || str.charAt(0) == '.') {
                    prefix = str.substring(0, 1);
                    str = str.substring(1);
                }
                var isAllZero = true;
                for(var i = 0; i < str.length; i++) {
                    if(str.charAt(i) != '0') {
                        isAllZero = false;
                        idx = i;
                        break;
                    }
                }

                var result = prefix;
                if(idx != 0) {
                    result += str.substring(idx); 
                } else {
                    // all zero
                    if(str.length > 0 && isAllZero) {
                        result += "";
                    } else {
                        result += str
                    }
                }

                return result;
            }

            function format_number(nStr ){
                nStr += '';  
                var x = nStr.split('.');  
                var x1 = x[0];  
                var x2 = x.length > 1 ? '.' + x[1] : '';  
                var rgx = /(\d+)(\d{3})/;
                
                x1 = removeZero(x1);
                if(x1 == '-') {
                    if(x.length == 1) {
                        x1 = "0";
                    } else {
                        x1 = "-0";
                    }
                } else if(x1 == '') {
                    x1 = 0;
                }

                while (rgx.test(x1)) {  
                    x1 = x1.replace(rgx, '$1' + ',' + '$2');  
                }  
                return x1 + x2;  
            }

            function toSingleBytes(str)
            {
                var result="";
                for (var i = 0; i < str.length; i++){
                    if (str.charCodeAt(i)==12288){
                        result+= String.fromCharCode(str.charCodeAt(i)-12256);
                        continue;
                    }
                    if (str.charCodeAt(i)>65280 && str.charCodeAt(i)<65375){
                        result+= String.fromCharCode(str.charCodeAt(i)-65248);
                    }else{
                        result+= String.fromCharCode(str.charCodeAt(i));
                    }
                }
                return result;
            }

            function changeHander(rawValue) {
                if ('undefined' === typeof(rawValue) || rawValue.length === 0) {
                    // return empty string
                    return "";	
                }
                // if it is an evaluation expression, just return itself back
                if (rawValue.charAt(0) === '=') {
                    return rawValue;
                }
                // first convert to single Bytes
                var v = toSingleBytes(rawValue);
                // then replace all comma
                v = v.replace(/,/g, "")
                // lastly check if it is a valid number using regex
                var re = /^(-?\d+)(\.\d+)?$/;
                if (!re.test(v)) {
                    v = "";
            　　} else {
                    v = format_number(v);
                }
                return v;
            }
        </script>
        
    </head>
    <body>
        <input id="mynum" type="text" size="20" onchange="this.value = changeHander(this.value);"/>
        <button onclick="alert(toThousands('abc12345678.33333'));">Click me</button>
    </body>
</html>